package com.ww.springboot.boot.util.pdf;


import com.lowagie.text.Document;
import com.lowagie.text.pdf.PdfCopy;
import com.lowagie.text.pdf.PdfImportedPage;
import com.lowagie.text.pdf.PdfReader;

import java.io.FileOutputStream;

public class PdfMergeUtil {

    /**
     * 筛选
     *
     * @param file    需要合并的pdf路径
     * @param newfile 合并成新的文件的路径
     */
    public static boolean delete(String file, String newfile, int[] indexs) {
        boolean retValue = false;
        Document document = null;
        PdfCopy copy = null;
        PdfReader reader = null;
        try {
            document = new Document(new PdfReader(file).getPageSize(1));
            copy = new PdfCopy(document, new FileOutputStream(newfile));
            document.open();

            reader = new PdfReader(file);
            //int n = reader.getNumberOfPages();
            for (int j = 0; j < indexs.length; j++) {
                document.newPage();
                PdfImportedPage page = copy.getImportedPage(reader, indexs[j]);
                copy.addPage(page);
            }
            reader.close();

            retValue = true;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (reader != null) {
                reader.close();
            }
            if (copy != null) {
                copy.close();
            }
            if (document != null) {
                document.close();
            }
        }
        return retValue;
    }


    /**
     * 合并pdf
     *
     * @param files   需要合并的pdf路径
     * @param newfile 合并成新的文件的路径
     */
    public static boolean mergePdfFiles(String[] files, String newfile) {
        boolean retValue = false;
        Document document = null;
        PdfCopy copy = null;
        PdfReader reader = null;
        try {
            document = new Document(new PdfReader(files[0]).getPageSize(1));
            copy = new PdfCopy(document, new FileOutputStream(newfile));
            document.open();
            for (int i = 0; i < files.length; i++) {
                reader = new PdfReader(files[i]);
                int n = reader.getNumberOfPages();
                for (int j = 1; j <= n; j++) {
                    document.newPage();
                    PdfImportedPage page = copy.getImportedPage(reader, j);
                    copy.addPage(page);
                }
                reader.close();
            }
            retValue = true;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (reader != null) {
                reader.close();
            }
            if (copy != null) {
                copy.close();
            }
            if (document != null) {
                document.close();
            }
        }
        return retValue;
    }

    public static void main(String[] args) {
        delete("C:\\Users\\ZH500\\桌面\\test\\xxx.pdf", "C:\\Users\\ZH500\\桌面\\pdf\\new.pdf", new int[]{3, 4, 5});
    }
}
